package com.itany.boot.service.Impl;

import com.itany.boot.constant.DictConstant;
import com.itany.boot.dao.ChapterMapper;
import com.itany.boot.dao.ResourceMapper;
import com.itany.boot.entity.Chapter;
import com.itany.boot.entity.ChapterExample;
import com.itany.boot.entity.Resource;
import com.itany.boot.entity.ResourceExample;
import com.itany.boot.exception.ChapterExistException;
import com.itany.boot.exception.FileUploadErrorException;
import com.itany.boot.exception.RecourceExistException;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.ChapterService;
import com.itany.boot.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @date 2025/10/17
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ChapterServiceImpl implements ChapterService {

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public void addChapter(String title, String inFo) throws RequestParameterErrorException, ChapterExistException {
        if(ParameterUtil.isNull(title) || ParameterUtil.isNull(inFo)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        ChapterExample chapterExample = new ChapterExample();
        chapterExample.or()
                .andTitleEqualTo(title);
        List<Chapter> chapters = chapterMapper.selectByExample(chapterExample);
        if(!chapters.isEmpty()) {
            throw new ChapterExistException("章节标题已经存在");
        }
        Date date = new Date();
        Chapter chapter = new Chapter();
        chapter.setTitle(title);
        chapter.setInfo(inFo);
        chapter.setCreateDate(date);
        chapter.setStatus(Integer.parseInt(DictConstant.COURSEREOURCESET_STATUS_DISABLED));
        chapterMapper.insertSelective(chapter);

    }

    @Override
    public void addResource(CommonsMultipartFile file, String rtitle, String costType, String costNumber, String fileType, HttpSession session) throws RequestParameterErrorException, RecourceExistException, FileUploadErrorException {
        if(file.isEmpty() || ParameterUtil.isNull(rtitle) || ParameterUtil.isNull(costType) || ParameterUtil.isNull(costNumber) || ParameterUtil.isNull(fileType)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        ResourceExample resourceExample = new ResourceExample();
        resourceExample.or()
                .andTitleEqualTo(rtitle);
        List<Resource> resources = resourceMapper.selectByExample(resourceExample);
        if(!resources.isEmpty()) {
            throw new RecourceExistException("资源标题已经存在");
        }

        Resource resource = new Resource();
        resource.setTitle(rtitle);
        resource.setCostType(Integer.parseInt(costType));
        resource.setCostNumber(Integer.parseInt(costNumber));
        resource.setFileType(fileType);

        Date date = new Date();
        String path = DictConstant.COURSERESOURCE_ROOT_FOLDER + new SimpleDateFormat("yyyyMMdd").format(date);
        String cp = session.getServletContext().getRealPath(path);
        File f = new File(cp);
        f.mkdirs();

        resource.setPath(path + File.separator + file.getOriginalFilename());

        try {
            resourceMapper.insertSelective(resource);
            file.transferTo(new File(cp,file.getOriginalFilename()));
        } catch (IOException e) {
            throw new FileUploadErrorException("资源上传出错");
        }
    }
}
